import { Component, Node, v3 } from 'cc';
import { Animatable } from './Animatable';
import { Dot } from '../component/Dot';

export class AsteroidDeathView extends Component implements Animatable {
    private static numDots = 8;

    public radius: number;

    private _dots: Dot[] = [];

    private _tmp = v3();

    protected onLoad(): void {
        const len = AsteroidDeathView.numDots;
        for (let i = 0; i < len; i += 1) {
            const node = new Node();
            const dot = node.addComponent(Dot);
            dot.maxDistance = this.radius;
            this.node.addChild(node);
            this._dots.push(dot);
        }
    }

    public animate(time: number): void {
        for (const dot of this._dots) {
            const node = dot.node;
            node.setPosition(this._tmp.set(node.position.x + dot.velocityX * time, node.position.y + dot.velocityY * time));
        }
    }
}
